package de.gwtprime.client.activities;

import java.util.List;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.Request;
import com.google.web.bindery.requestfactory.shared.ServerFailure;

import de.gwtprime.client.ClientFactory;
import de.gwtprime.client.PrimeRequestFactory;
import de.gwtprime.client.event.VotesLoadedEvent;
import de.gwtprime.client.event.VotesSectionCompleteLoadedEvent;
import de.gwtprime.client.places.ArticlePlace;
import de.gwtprime.client.util.AppUtils;
import de.gwtprime.client.util.ErrorWidget;
import de.gwtprime.client.util.UserInputParser;
import de.gwtprime.client.views.ArticleView;
import de.gwtprime.client.viewsStatic.LoadingIndictator;
import de.gwtprime.client.viewsStatic.NavigationViewImpl.Section;
import de.gwtprime.shared.requestFactory.ArticleProxy;
import de.gwtprime.shared.requestFactory.VoteProxy;
import de.gwtprime.shared.requestFactory.VoteSectionCompletedProxy;
import de.gwtprime.shared.requestFactory.request.ArticleRequest;
import de.gwtprime.shared.util.AppUtilsShared;

public class ArticleActivity extends PrimeAbstractActivity implements
		ArticleView.Presenter {

	private ClientFactory clientFactory;

	private String name;

	/**
	 * Logger
	 */
	// private static Logger logger =
	// Logger.getLogger(ArticleActivity.class.getName());

	public ArticleActivity(ArticlePlace place, ClientFactory clientFactory) {
		this.name = place.getName();
		this.clientFactory = clientFactory;
	}

	ArticleView articleView;

	@Override
	public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
		articleView = clientFactory.getArticleView();
		panel.setWidget(new LoadingIndictator());

		clientFactory.getNavigationView().activateSection(Section.ARTICLE);

		final PrimeRequestFactory requestFactory = clientFactory
				.getRequestFactory();

		final ArticleRequest request = requestFactory.articleRequest();
		
		Request<ArticleProxy> createReq = null;
		
		if(AppUtilsShared.enableNextPrevArticleBtn){
			createReq = request.findArticle(
				Long.parseLong(name)).with("sections", "comments.user", "nextArticle", "prevArticle");
		}else{
			createReq = request.findArticle(
					Long.parseLong(name)).with("sections", "comments.user");
		}
		createReq.fire(new Receiver<ArticleProxy>() {
			@Override
			public void onSuccess(ArticleProxy article) {
				
				if(article==null){
					panel.setWidget(new ErrorWidget("Article was not found"));
					return;
				}
				
				panel.setWidget(articleView.asWidget());
				articleView.setPresenter(ArticleActivity.this);
				articleView.setArticle(article);
				UserInputParser.prettyPrint();
				
				loadVotes(article);

				// initialize all google plus widgets on the articles sections
				AppUtils.initializeGooglePlusWidget("");
				
				AppUtils.trackPageview("Article:"+article.getId());
			}

			private void loadVotes(ArticleProxy article) {
				if(clientFactory.getUser()!=null){
					
					final ArticleRequest votesCompReq = requestFactory.articleRequest();
					Request<List<VoteSectionCompletedProxy>> createVotesCompReq = votesCompReq.getAllSectionVotesCompleteForUser(clientFactory.getUser()).using(article);
					createVotesCompReq.fire(new Receiver<List<VoteSectionCompletedProxy>>() {
						@Override
						public void onSuccess(List<VoteSectionCompletedProxy> votes) {
							clientFactory.getEventBus().fireEvent(new VotesSectionCompleteLoadedEvent(votes));
						}

						@Override
						public void onFailure(ServerFailure error) {
							AppUtils.log("Failed to load section complete votes");
						}
					});
					
					
					final ArticleRequest request2 = requestFactory.articleRequest();
					Request<List<VoteProxy>> createReq2 = request2.getAllSectionVotesForUser(clientFactory.getUser()).using(article);
					createReq2.fire(new Receiver<List<VoteProxy>>() {
						@Override
						public void onSuccess(List<VoteProxy> votes) {
							clientFactory.getEventBus().fireEvent(new VotesLoadedEvent(votes));
						}

						@Override
						public void onFailure(ServerFailure error) {
							AppUtils.log("Failed to load votes");
						}
					});
				}
			}

			@Override
			public void onFailure(ServerFailure error) {
				panel.setWidget(new ErrorWidget("An error occured while loading the article"));
				AppUtils.log("Failed to load article: "+error);
			}
		});
		
	}

	@Override
	public void goTo(Place place) {
		clientFactory.getPlaceController().goTo(place);
	}

}
